/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.HisDiagnosis;
import com.jims.master.modules.doctor.entity.LiveRequisition;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.*;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 住院信息Controller
 * @author chentao
 * @version 2017-03-10
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/liveRequisition")
public class LiveRequisitionController extends BaseController {

	@Autowired
	private LiveRequisitionService liveRequisitionService;
	@Autowired
	private ClinicMasterService clinicMasterService;
	@Autowired
	private PatVisitService patVisitService;
	@Autowired
	private HisDiagnosisService hisDiagnosisService;
	
	@ModelAttribute
	public LiveRequisition get(@RequestParam(required=false) String id) {
		LiveRequisition entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = liveRequisitionService.get(id);
		}
		if (entity == null){
			entity = new LiveRequisition();
		}
		return entity;
	}

	/**
	 * 入院患者登记列表
	 * @param liveRequisition
	 * @param request
	 * @param response
	 * @param model
	 * @author 陈涛
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"hospitalRegistr", ""})
	public String list(LiveRequisition liveRequisition,PatVisit patVisit, HttpServletRequest request, HttpServletResponse response, Model model) {
		String orgId = UserUtils.getOrgId();
		patVisit.setOrgId(orgId);
		patVisit.setInFlag("0");
		Page<PatVisit> page = patVisitService.findPageVist(new Page<PatVisit>(request, response), patVisit);
		model.addAttribute("page", page);
		return "modules/doctor/hospitalRegistr";
	}

	/**
	 * 入院登记页面
	 * @param liveRequisition
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"hospitalRegistrFrom", ""})
	public String listRegistr(LiveRequisition liveRequisition,PatVisit patVisit, HttpServletRequest request, HttpServletResponse response, Model model){
		String orgId = UserUtils.getOrgId();
		String userName = UserUtils.getUser().getName();
		liveRequisition.setOrgId(orgId);
		Page<LiveRequisition> page = liveRequisitionService.findPage(new Page<LiveRequisition>(request, response), liveRequisition);
		model.addAttribute("page", page);
		model.addAttribute("userName", userName);
		model.addAttribute("patVisit", patVisit);
		return "modules/doctor/hospitalRegistrFrom";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"hospitalRegistrLeft", ""})
	public String hospitalRegistrLeft(LiveRequisition liveRequisition,HttpServletRequest request, HttpServletResponse response, Model model ){
		List<HisDiagnosis> hisDiagnosisList = hisDiagnosisService.getByClinicId(liveRequisition.getClinicId(),"");
		String hisDiagnosisDes = "";
		for(int i = 0; i<hisDiagnosisList.size(); i++){
			hisDiagnosisDes += hisDiagnosisList.get(i).getIcdName()+";  ";
		}
		model.addAttribute("liveRequisition", liveRequisition);
		model.addAttribute("hisDiagnosisDes", hisDiagnosisDes);
		return "modules/doctor/hospitalRegistrLeft";
	}
	/**
	 * 住院通知单
	 * @param request
	 * @param response
	 * @param model
	 * @author chentao
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(String clinicId, String patientId, HttpServletRequest request, HttpServletResponse response, Model model) {
		LiveRequisition liveRequisition =  liveRequisitionService.getByClinicId(clinicId);
		ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
		List<HisDiagnosis> hisDiagnosisList = hisDiagnosisService.getByClinicId(clinicId,"");
		String hisDiagnosisDes = "";
		for(int i = 0; i<hisDiagnosisList.size(); i++){
			hisDiagnosisDes += hisDiagnosisList.get(i).getIcdName()+";  ";
		}
		model.addAttribute("clinicMaster", clinicMaster);
		model.addAttribute("liveRequisition", liveRequisition);
		model.addAttribute("hisDiagnosisDes",hisDiagnosisDes);
		return "modules/doctor/liveRequisitionIndex";
	}

	/**
	 * 住院通知单打印
	 * @param liveRequisition
	 * @param request
	 * @param response
	 * @param model
	 * @author chentao
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "hospitalNotice")
	public String print(LiveRequisition liveRequisition,String clinicId,HttpServletRequest request, HttpServletResponse response, Model model) {
		liveRequisition =  liveRequisitionService.getByClinicId(clinicId);
		model.addAttribute("liveRequisition",liveRequisition);
		return "modules/doctor/hospitalNotice";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(LiveRequisition liveRequisition, Model model) {
        PatVisit  patVisit=  patVisitService.get(liveRequisition.getId());
        model.addAttribute("patVisit", patVisit);
		return "modules/doctor/liveRequisitionForm";
	}

	/**
	 * 住院通知单保存
	 * @param liveRequisition
	 * @param
	 * @return
	 * @author chentao
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(LiveRequisition liveRequisition) {
		int i = 0;
	    StringData data=new StringData();
		liveRequisition.updateOrgId();
		i=liveRequisitionService.save(liveRequisition);
        if(i>0){
            data.setCode("success");
            data.setData("保存住院信息成功");
        }else{
            data.setCode("error");
            data.setData("保存住院信息失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(LiveRequisition liveRequisition) {
	    StringData data=new StringData();
		int i=liveRequisitionService.delete(liveRequisition);
        if(i>0){
            data.setCode("success");
            data.setData("删除住院信息成功");
        }else{
            data.setCode("error");
            data.setData("删除住院信息失败");
        }
		return data;
	}


	@RequiresPermissions("user")
	@RequestMapping(value = "printLive")
	@ResponseBody
	public StringData printLive(LiveRequisition liveRequisition) {
		String printPath = liveRequisitionService.printLive(liveRequisition,UserUtils.getUser());
		StringData stringData = new StringData();
		stringData.setData(printPath);
		stringData.setCode("success");
		return stringData;
	}

}